package friendsbtt.utils.google;

import java.io.IOException;
import java.util.List;

import com.google.gdata.data.photos.AlbumEntry;
import com.google.gdata.data.photos.AlbumFeed;
import com.google.gdata.data.photos.GphotoEntry;
import com.google.gdata.data.photos.PhotoEntry;
import com.google.gdata.util.ServiceException;

import friendsbtt.core.lang.CustomList;
import friendsbtt.utils.IAlbum;
import friendsbtt.utils.IPhoto;

public class Album implements IAlbum {

	private AlbumEntry baseAlbum;
	
	private CustomList<IPhoto> photos;
	
	public Album(AlbumEntry baseAlbum){
		this.baseAlbum = baseAlbum;
	}
	
	@Override
	public CustomList<IPhoto> getPhotos() {
		if(photos == null){
			setPhotos();
		}
		return photos;
	}
	
	private void setPhotos(){
		try {
			AlbumFeed feed = this.baseAlbum.getFeed(AlbumFeed.class);
			List<GphotoEntry> entries = feed.getEntries();
		    photos = new CustomList<IPhoto>();
		    for (GphotoEntry entry : entries) {
		      GphotoEntry adapted = entry.getAdaptedEntry();
		      if (adapted instanceof PhotoEntry) {
		        photos.add(new Photo((PhotoEntry) adapted));
		      }
		    }
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ServiceException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public String getTitle() {
		return this.baseAlbum.getTitle().getPlainText();
	}
}
